<?php
/**
 * Class FormRegistration created by Pixoid.
 *
 * @version 1.0
 */
class FormUserFilter extends UserFilter {

    public $config = array();
    public $tags;

    public function init() {
        $this->config = array(
            'class' => 'form',
            'elements' => array(
                'name' => array('type' => 'text'),
                'tags' => array('type' => 'text'),
            ),
            'buttons' => array(
                'send' => array('type' => 'htmlSubmit', 'value' => 1, 'class' => 'blue', 'label' => '<span class="label">Создать фильтр</span>'),
            )
        );
        return parent::init();
    }

    public function rules() {
        return array(
            array('name,tags', 'required'),
            array('tags', 'send'),
        );
    }

    public function attributeLabels() {
        return array(
            'name' => 'Название фильтра',
            'tags' => 'Теги (через запятую)',
        );
    }

    public function send($attribute, $params) {
        if(!$this->hasErrors()) {
            $ids = Tags::addNewTags($this->tags);
            $filter = new UserFilter();
            $filter->owner_id = Y::userId();
            $filter->name = $this->name;
            $filter->tag_ids = join(", ", $ids);
            $filter->save();
        }
    }

}
